package com.leesche.zcsp.node.pay.wecha.service.encha.impl;

import com.leesche.zcsp.node.net.service.NetService;
import com.leesche.zcsp.node.pay.wecha.data.RequestParam;
import com.leesche.zcsp.node.pay.wecha.data.ToPayCondition;
import com.leesche.zcsp.node.pay.wecha.listener.AuthorListener;
import com.leesche.zcsp.node.pay.wecha.service.PayService;
import com.leesche.zcsp.node.utils.PayConfig;
import com.leesche.zcsp.node.utils.RequestHandler;
import com.leesche.zcsp.node.utils.TenpayUtil;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.model.SysMember;
import com.leesche.zcsp.node.web.model.SysUser;
import com.leesche.zcsp.node.web.service.*;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QuerySysMemberCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.SysMemberDto;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Service
public class PayServiceImpl implements PayService {

    public final static String NO_CHECK = "NO_CHECK";
    public final static String FORCE_CHECK = "FORCE_CHECK";
    public final static String OPTION_CHECK = "OPTION_CHECK";
    private static final Logger LOGGER = Logger.getLogger(PayServiceImpl.class);
    public AuthorListener openListener;
    @Autowired
    ServOrderService serviceOrderService;
    @Autowired
    PubDiscountService pubDiscountService;
    @Autowired
    PubDeviceService pubDeviceService;
    @Autowired
    NetService netService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysMemberService sysMemberService;

    public void execListener(ToPayCondition condition, HttpServletResponse response) throws IOException {
        openListener.listen(condition, response);
    }

    public void addAuthorListener(AuthorListener listener) {
        openListener = listener;
    }


    /**
     * @param deviceid;
     * @param discountid
     * @param encode
     * @return 返回url
     * @Description: 生成微信二维码农
     * @author 陈辉洪
     */
    public String generateURL(String deviceid, String discountid, String encode) {

        RequestHandler reqHandler = new RequestHandler(encode);
        reqHandler.init(PayConfig.appid, PayConfig.appsecret, PayConfig.partnerkey);

        RequestParam param = new RequestParam(reqHandler);
        param.putParam("appid", PayConfig.appid);
        param.putParam("mch_id", PayConfig.partner);
        param.putParam("nonce_str", TenpayUtil.getNonceStr());

        StringBuffer buffer = new StringBuffer();
        buffer.append(deviceid);
        buffer.append(Constants.SPILTTMARK);
        buffer.append(discountid);

        param.putParam("product_id", buffer.toString());
        /*param.putParam("time_stamp", Sha1Util.getTimeStamp());*/
        param.putParam("time_stamp", "12345");

        StringBuffer url = new StringBuffer();
        url.append(PayConfig.bizpayurl);
        url.append("?");
        url.append(param.toString());
        return url.toString();
    }

    /**
     * @param deviceid
     * @param discountid
     * @return 返回url
     * @Description: 生成二维码
     * @author 陈辉洪
     */
    @Override
    public String generaterAlipay(String deviceid, String discountid) {
        StringBuffer urlBuf = new StringBuffer();
        urlBuf.append("http://m.leesche.cn/node/alipay?");
        urlBuf.append("deviceid=");
        urlBuf.append(deviceid);
        urlBuf.append("&");
        urlBuf.append("discountid=");
        urlBuf.append(discountid);
        return urlBuf.toString();
    }

    @Override
    public boolean isWechatBrower(String userAgent) {
        String lowerCase = userAgent.toLowerCase();
        return lowerCase.indexOf("micromessenger") >= 0;
    }

    @Override
    public boolean isAlipayBrower(String userAgent) {
        String lowerCase = userAgent.toLowerCase();
        return lowerCase.indexOf("alipay") >= 0;
    }

}
